package com.MinesweeperGame.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;


public class MinesweeperRegistrationForm {
	
    Label LabelRegistrationForm; 
	Label LabelUserName;
	Label LabelPassword;
	Label LabelConfirmPassword;
	
	TextBox Textbox_UserName;
	PasswordTextBox PasswordTextbox_Password;
	PasswordTextBox PasswordTextbox_ConfirmPassword;
	
	Button ButtonRegister = new Button("Register",new ClickHandler() {
		
		@Override
		public void onClick(ClickEvent event) {
			
			add();
		}
	});
	
	Button ButtonCancel = new Button("Cancel", new ClickHandler() {
		
		@Override
		public void onClick(ClickEvent event) {
			
			PopupPanel_RegistrationForm.hide();
		}
	});
	
	PopupPanel PopupPanel_RegistrationForm = new PopupPanel();
	
	private final GreetingServiceAsync greetService = GWT
			.create(GreetingService.class);
	
	
	public MinesweeperRegistrationForm(){
		
		 AbsolutePanel absolutePanel = new AbsolutePanel();
		 absolutePanel.setWidth("200px");
		 
		 
		 
		 LabelRegistrationForm = new Label("REGISTRATION FORM");
		 LabelUserName = new Label("UserName");
		 Textbox_UserName = new TextBox();
		 LabelPassword = new Label("Password");
		 PasswordTextbox_Password = new PasswordTextBox();
		 LabelConfirmPassword = new Label("ConfirmPassword");
		 PasswordTextbox_ConfirmPassword = new PasswordTextBox();
		 
		 absolutePanel.add(LabelRegistrationForm);  
		 absolutePanel.add(LabelUserName);
		 absolutePanel.add(Textbox_UserName);
		 absolutePanel.add(LabelPassword);
		 absolutePanel.add(PasswordTextbox_Password);
		 absolutePanel.add(LabelConfirmPassword);
		 absolutePanel.add(PasswordTextbox_ConfirmPassword);
		 absolutePanel.add(ButtonRegister);
		 absolutePanel.add(ButtonCancel);
    
		 PopupPanel_RegistrationForm.add(absolutePanel);
		 PopupPanel_RegistrationForm.setStyleName("register");
		 LabelRegistrationForm.setStyleName("minesweeperRegister");
		 
    
	}
	
	public void show() {
	
		PopupPanel_RegistrationForm.show();
		PopupPanel_RegistrationForm.center(); 
	
	}

	public void add() {
	
	String password = PasswordTextbox_Password.getText();
	String confirmPassword = PasswordTextbox_ConfirmPassword.getText();
	
	if(Textbox_UserName.getText().equals("")) {
		Window.alert("Enter the UserName");
		}
		else if( PasswordTextbox_Password.getText().equals("")) {
			Window.alert("Enter the Password");
		}
	
		else if(PasswordTextbox_ConfirmPassword.getText().equals("")) {
			Window.alert("Confirm the Password");
		}
	   if(password.equals(confirmPassword) )   {
			
		   String uname = Textbox_UserName.getText();
			String pwd = PasswordTextbox_Password.getText();
			System.out.println(pwd);
			System.out.println(PasswordTextbox_ConfirmPassword.getText());
			greetService.addUser(uname, pwd, new AsyncCallback<String>() {
					public void onFailure(Throwable caught) {
						System.out.print("error");
					}

					public void onSuccess(String result) {
						Window.alert("User has been Registered");
						PopupPanel_RegistrationForm.hide();
						MinesweeperGame minesweeperGame = new MinesweeperGame();
						
						}
					});
				}
				else {
					
					Window.alert("Confirm Password Does not Match");
				}
  		}

}

